# -*- coding: utf-8 -*-
"""
Module Description: 配置表方法
Date: 2020/12/18
Author: Peng Ming Yuan
"""
from pyshare.const.lang.tips_info import TipsInfo
from pyshare.dal.gameconfig.query import getProfileList, getProfile
from pyshare.framework.error_handle import GameException
from collections import OrderedDict


def get_cfg_to_client(tc_name):
    """
    读取配置下发给客户端
    :param tc_name:   配置表名
    :return:
    """
    cfg_list = []
    cfg = getProfileList(tc_name)
    if not cfg:
        raise GameException(TipsInfo.RoleKeys.getCfgError)
    for c in cfg:
        cfg_list.append(c.asdict())
    return cfg_list


def get_show_cfg_dict(activity_type, item_type, **kwargs):
    """
    根据活动类型和道具类型读取外显配置
    :param activity_type:   活动类型
    :param item_type:   道具类型
    :param kwargs:   读取配置表的额外参数
    :return: {item_id: 配置对象} eg:{1: cfg_object}
    """
    tc_table_name, key_name = get_table_by_item_type(item_type)
    show_cfg_list = getProfileList(tc_table_name, ActivityType=activity_type,  **kwargs)
    show_cfg_dict = OrderedDict()
    for show_cfg in show_cfg_list:
        show_cfg_dict[getattr(show_cfg, key_name)] = show_cfg
    return show_cfg_dict


def get_table_by_item_type(item_type):
    """
    根据道具类型读取配置表名
    :param item_type:
    :return:
    """
    type_cfg = getProfile("TcItemType", ItemType=item_type)
    if not type_cfg:
        raise GameException(TipsInfo.RoleKeys.getCfgError)
    tc_table_name = type_cfg.CfgTable
    key_name = type_cfg.KeyName
    return tc_table_name, key_name


